/**
 * 平台公告
 */

//图片前置url
var pre = imageUrl;

//平台公告
$(function () {

    $.ajax({
        type: "GET",
        url: restUrl + "/index/items",   //接口地址
        dataType: "json",
        data: {
            parentCode: "TPGG",
            loadAll: false,
            needPaging: true,
            currPage: 1,
            pageSize: 5
        },
        success: function (v) {
            if (v.code == 10000) {
                searchDataSuccessCallback(v.data);

            } else {
                console.log(v.message)
            }
        },
        error:function (data) {
            console.log(data)
        }
    })
});

//转换日期格式
function formatDate(v) {
    if (/^(-)?\d{1,10}$/.test(v)) {
        v = v * 1000;
    } else if (/^(-)?\d{1,13}$/.test(v)) {
        v = v * 1;
    }
    var dateObj = new Date(v);
    var month = dateObj.getMonth() + 1;
    var day = dateObj.getDate();
    var hours = dateObj.getHours();
    var minutes = dateObj.getMinutes();
    var seconds = dateObj.getSeconds();
    if (month < 10) {
        month = "0" + month;
    }
    if (day < 10) {
        day = "0" + day;
    }
    <!--if(hours<10){-->
    <!--hours = "0" + hours;-->
    <!--}-->
    <!--if(minutes<10){-->
    <!--minutes = "0" + minutes;-->
    <!--}-->
    <!--if(seconds<10){-->
    <!--seconds = "0" + seconds;-->
    <!--}-->
    <!--var UnixTimeToDate = dateObj.getFullYear() + '-' + month + '-' +day + ' ' + hours + ':' + minutes + ':' + seconds;-->
    var UnixTimeToDate = dateObj.getFullYear() + '-' + month + '-' + day + ' ';
    return UnixTimeToDate;
}

// 全局变量：分页组件是否存在，如果存在就不需要初始化，如果不存在需要初始化
var paginationFlag = false;
/**
 * 分页查询之后的成功回调
 * @param data
 */

function searchDataSuccessCallback(data) {

    //分页初始化
    var paginationParams = {total: data.total, pageSize: data.pageSize};
    if (!paginationFlag) {
        paginationFlag = true;
        paginationInit(paginationParams, changePagination);
    }

    $('#contentarea').empty();
    for (var i = 0; i < data.rows.length; i++) {
        var item = data.rows[i];
        if(item.title.indexOf('\n') >=0){ item.title = item.title.replace(/\n/g,"");}

        $('#contentarea').append('<div class="news wow fadeInUp" id="' + item.id + '"><img src=' + pre + item.cover + '>' +
            '<div class="con"><a href="info.html?id=' + item.id + '&type=TPGG">' + item.title + '</a><span>' + formatDate(item.publishDate) + '</span>' +
            '<div class="small_font">' + item.description + '</div></div><div class="clearfix">' + '</div></div>');
        $('.news').on('click', function () {
            var id = $(this).attr('id');

            window.location.href = "info.html?id=" + id + "&type=TPGG";
        })
    }
}

/**
 * 分页组件发生改变之后回调
 * @param paginationParams
 */
function changePagination(paginationParams) {

    searchData(paginationParams);
}

// 默认当前页面
var initCurrPage = 1;
// 默认每页大小
var initPageSize = 2;
// 查询数据
function searchData(paramData) {
    if (!paramData) {
        paramData = {};
    }
    paramData.parentCode="TPGG";
    paramData.loadAll=false;
    paramData.needPaging=true;
    paramData.currPage = paramData.currentPage ? paramData.currentPage : initCurrPage;
    paramData.pageSize = paramData.pageSize ? paramData.pageSize : initPageSize;
    let params = {
        url:"/index/items",
        data:paramData,
        sCallback: function(data){
            searchDataSuccessCallback(data);
        }
    }
    requset(params);
}

